package com.marvels.financial.loan.service.impl;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.marvels.approve.loan.dto.McfeCustomerLoanContract;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.constants.RedisKeyConstant;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.api.IProductInfoApi;
import com.marvels.financial.loan.dao.McfeCustomerLoanContractDao;
import com.marvels.financial.loan.dto.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.marvels.common.constants.ExtensionFunctionEnum;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.DateUtils;
import com.marvels.financial.loan.dao.McfeCustomerRepaymentPlanDao;
import com.marvels.financial.loan.dao.McfeRepaymentFlowDao;
import com.marvels.financial.loan.service.LendPaymentApi;



@Service
public class LendPaymentImpl implements LendPaymentApi{

	@Autowired
	private McfeCustomerRepaymentPlanDao repaymentPlanDao;

	@Autowired
	private McfeRepaymentFlowDao repaymentFlowDao;

	@Autowired
	private McfeRepaymentFlowDao flowDao;
	@Autowired
	private McfeCustomerLoanContractDao mcfeCustomerLoanContractDao;
	@Autowired
	private IProductInfoApi iProductInfoApi;

	@Override
	public Map<String, Object> paymentPlanList(LendPaymentConditionDTO conditionDTO) {
		int pageSize=conditionDTO.getPageSize();
		int pageNum=conditionDTO.getPageNum();
		if ( pageSize< 1) {
			pageSize=PageConstant.PAGESIZE;
		}
		if (pageNum< 1) {
			pageNum=PageConstant.CURRPAGE;
		}
		PageHelper.startPage(pageNum, pageSize);
		Page<LendPaymentPlanDTO> list=repaymentPlanDao.paymentPlanList(conditionDTO);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data",list);
		map.put("total",list.getTotal());
		LendPaymentPlanDTO dto=repaymentPlanDao.paymentPlanAmountStatic(conditionDTO);
		List<LendPaymentPlanDTO> staticList=new ArrayList<LendPaymentPlanDTO>();
		staticList.add(dto);
		map.put("static",staticList);
		//查询统计数据
		return map;
	}

	@Override
	public HSSFWorkbook downPaymentPlan(LendPaymentConditionDTO conditionDTO) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("还款计划数据一览表");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("进件号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("客户");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("身份证号");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("手机号");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("银行帐号");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("开户行");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("产品");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("贷款日期");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("放款金额");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("还款日期");
		cell.setCellStyle(style);
		cell = row.createCell(11);
		cell.setCellValue("最迟还款日期");
		cell.setCellStyle(style);
		cell = row.createCell(12);
		cell.setCellValue("已还金额");
		cell.setCellStyle(style);
		cell = row.createCell(13);
		cell.setCellValue("剩余应还");
		cell.setCellStyle(style);
		cell = row.createCell(14);
		cell.setCellValue("实际还款日期");
		cell.setCellStyle(style);
		cell = row.createCell(15);
		cell.setCellValue("逾期费");
		cell.setCellStyle(style);
		cell = row.createCell(16);
		cell.setCellValue("状态");
		cell.setCellStyle(style);
		BigDecimal loanAmountTotal=new BigDecimal("0.00");//贷款总金额
		BigDecimal paymentAmountTotal=new BigDecimal("0.00");//还款总金额
		BigDecimal paymentAmountFjTotal=new BigDecimal("0.00");//剩余应还
		BigDecimal overdueAmountTotal=new BigDecimal("0.00");//逾期费
		Page<LendPaymentPlanDTO> list=repaymentPlanDao.paymentPlanList(conditionDTO);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(i+1);
				row.createCell(1).setCellValue(list.get(i).getLoanId());
				row.createCell(2).setCellValue(list.get(i).getCustName());
				row.createCell(3).setCellValue(list.get(i).getCustCardNo());
				row.createCell(4).setCellValue(list.get(i).getMobileNumber());
				row.createCell(5).setCellValue(list.get(i).getBankCard());
				row.createCell(6).setCellValue(list.get(i).getBankName());
				row.createCell(7).setCellValue(list.get(i).getProductTypeName());
				row.createCell(8).setCellValue(DateUtils.getDateStr(list.get(i).getLoanDate(), "yyyy-MM-dd"));
				row.createCell(9).setCellValue(list.get(i).getLoanAmount()+"");
				row.createCell(10).setCellValue(DateUtils.getDateStr(list.get(i).getPaymentDate(), "yyyy-MM-dd"));
				row.createCell(11).setCellValue(DateUtils.getDateStr(list.get(i).getPaymentDateLate(), "yyyy-MM-dd"));
				row.createCell(12).setCellValue(list.get(i).getPaymentAmount()+"");
				row.createCell(13).setCellValue(list.get(i).getPaymentAmountPeriod()+"");
				row.createCell(14).setCellValue(list.get(i).getPaymentDateAct());
				row.createCell(15).setCellValue(list.get(i).getOverdueAmount()+"");
				if("0".equals(list.get(i).getStatus())){
					row.createCell(16).setCellValue("正常");
				}else if("1".equals(list.get(i).getStatus())){
					row.createCell(16).setCellValue("逾期");
				}else if("2".equals(list.get(i).getStatus())){
					row.createCell(16).setCellValue("结清");
				}else if("3".equals(list.get(i).getStatus())){
					row.createCell(16).setCellValue("提前结清");
				}else if("10".equals(list.get(i).getStatus())){
					row.createCell(16).setCellValue("核销");
				}
				loanAmountTotal=loanAmountTotal.add(list.get(i).getLoanAmount());
				paymentAmountTotal=paymentAmountTotal.add(list.get(i).getPaymentAmount());
				paymentAmountFjTotal=paymentAmountFjTotal.add(list.get(i).getPaymentAmountPeriod());
				overdueAmountTotal=overdueAmountTotal.add(list.get(i).getOverdueAmount());
			}
		}
		row = sheet.createRow(list.size() + 1);
		row.createCell(0).setCellValue("合计");
		row.createCell(9).setCellValue(loanAmountTotal+"");
		row.createCell(12).setCellValue(paymentAmountTotal+"");
		row.createCell(13).setCellValue(paymentAmountFjTotal+"");
		row.createCell(15).setCellValue(overdueAmountTotal+"");
		return wb;

	}

	@Override
	public MarvelsResultDTO paymentFlowList(LendPaymentConditionDTO conditionDTO) {
		int pageSize=conditionDTO.getPageSize();
		int pageNum=conditionDTO.getPageNum();
		if ( pageSize< 1) {
			pageSize=PageConstant.PAGESIZE;
		}
		if (pageNum< 1) {
			pageNum=PageConstant.CURRPAGE;
		}
		BigDecimal paymentAmountTotal=new BigDecimal("0");
		BigDecimal principalTotal=new BigDecimal("0");
		BigDecimal interestTotal=new BigDecimal("0");
		BigDecimal manageFeeTotal=new BigDecimal("0");
		BigDecimal lateAmountTotal=new BigDecimal("0");
		BigDecimal violateAmountTotal=new BigDecimal("0");
		BigDecimal settleFeeTotal=new BigDecimal("0");
		BigDecimal paymentAmountActTotal=new BigDecimal("0");
		Page<RepaymentFlowListDTO> list=repaymentFlowDao.paymentFlowList(conditionDTO);
		if(null !=list && list.size()>0){
			for(RepaymentFlowListDTO dto:list){
				paymentAmountTotal=paymentAmountTotal.add(dto.getPaymentAmount());
				principalTotal=principalTotal.add(dto.getPrincipal());
				interestTotal=interestTotal.add(dto.getInterest());
				manageFeeTotal=manageFeeTotal.add(dto.getManageFee());
				lateAmountTotal=lateAmountTotal.add(dto.getLateAmount());
				violateAmountTotal=violateAmountTotal.add(dto.getViolateAmount());
				settleFeeTotal=settleFeeTotal.add(dto.getSettleFee());
				paymentAmountActTotal=paymentAmountActTotal.add(dto.getPaymentAmountAct());
			}
		}
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("list", list);
		map.put("total", list.getTotal());
		RepaymentFlowListDTO listDto=new RepaymentFlowListDTO();
		listDto.setPaymentAmount(paymentAmountTotal);
		listDto.setPrincipal(principalTotal);
		listDto.setInterest(interestTotal);
		listDto.setManageFee(manageFeeTotal);
		listDto.setLateAmount(lateAmountTotal);
		listDto.setViolateAmount(violateAmountTotal);
		listDto.setSettleFee(settleFeeTotal);
		listDto.setIndex("本页合计");
		listDto.setPaymentAmountAct(paymentAmountActTotal);
		List<RepaymentFlowListDTO> array=new ArrayList<>();
		array.add(listDto);
		map.put("statisticsData", array);
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), map);
	}


	@Override
	public MarvelsResultDTO newPaymentFlowList(LendPaymentConditionDTO conditionDTO) {
		int pageSize=conditionDTO.getPageSize();
		int pageNum=conditionDTO.getPageNum();
		if ( pageSize< 1) {
			pageSize=PageConstant.PAGESIZE;
		}
		if (pageNum< 1) {
			pageNum=PageConstant.CURRPAGE;
		}
		// 获取当前模式的展期费率
		conditionDTO.setExteniosOneDay(ExtensionFunctionEnum.vals()[0]);
		conditionDTO.setExteniosThreeDay(ExtensionFunctionEnum.vals()[1]);
		conditionDTO.setExteniosSevenDay(ExtensionFunctionEnum.vals()[2]);
		Page<RepaymentFlowListDTO> list = repaymentFlowDao.newPaymentFlowList(conditionDTO);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data",list);
		map.put("total",list.getTotal());
		RepaymentFlowListDTO dto = repaymentFlowDao.newPaymentFlowAmountStatic(conditionDTO);
		List<RepaymentFlowListDTO> staticList=new ArrayList<RepaymentFlowListDTO>();
		staticList.add(dto);
		map.put("static",staticList);
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), map);
	}

	@Override
	public void extensionApply(ReplacePaymentConditionDto replacePaymentConditionDto) throws MarvelsBusinessException {
		String loanId = replacePaymentConditionDto.getLoanId();
		// 加锁 避免重复插入数据
		String key = RedisKeyConstant.HAND_PAYMENT_WITHHOLD + loanId;
		boolean isNotLock = false;
		try {
			isNotLock = CacheUtils.isNotLock(key, loanId);
			if (isNotLock) {
				// 先将这个key设置过期时间
				CacheUtils.setExpire(key, 60);
				List<ReplacePaymentApplyInfo> list = mcfeCustomerLoanContractDao.selectContract(replacePaymentConditionDto);
				if (list == null || list.size() < 1) {
					throw new MarvelsBusinessException("合同信息不存在");
				}
				// 判断进件是否处于还款状态
				if (list.get(0).getContractStatus().equals("2") || list.get(0).getContractStatus().equals("3")
						|| list.get(0).getContractStatus().equals("10")) {
					throw new MarvelsBusinessException("进件不处于还款状态");
				}
				McptProduct productDtO = new  McptProduct();
				McfeCustomerLoanContractDTO mcfeCustomerLoanContractDTO = mcfeCustomerLoanContractDao.selectContractByLoanId(replacePaymentConditionDto.getLoanId());
				productDtO.setProductCode(mcfeCustomerLoanContractDTO.getProductCode());
				MarvelsResultDTO rateFeeDTO = iProductInfoApi.queryFeeRateByProductCode(productDtO);
				McptProductFundRelationDto mcptProductFundRelationDto = null;// 查询产品小类利率
				String josnStr = JSON.toJSONString(rateFeeDTO.getData());
				ProductMsgInfoDTO rateFeeMap = JSON.toJavaObject(JSON.parseObject(josnStr), ProductMsgInfoDTO.class);
				McptAmortMethodDTO mcptAmortMethodDTO = rateFeeMap.getMcptAmortMethodDTO();
				List<McptProductFundRelationDto> mcptProductFundRelationDtoList = (List<McptProductFundRelationDto>) rateFeeMap
						.getMcptProductFundRelationDto();
				if (mcptProductFundRelationDtoList != null && mcptProductFundRelationDtoList.size() > 0) {
					mcptProductFundRelationDto = mcptProductFundRelationDtoList.get(0);
				} else {
					mcptProductFundRelationDto = new McptProductFundRelationDto();
				}
				BigDecimal dayRate = mcptProductFundRelationDto.getRate().divide(BigDecimal.valueOf(365), MarvelsConstant.BIGDECIMAL_SCALE, BigDecimal.ROUND_HALF_UP);
				int roundFlag = 0;

				if (mcptAmortMethodDTO.getRoundUp()==MarvelsConstant.ROUND_HALF_UP ) {
					roundFlag = BigDecimal.ROUND_HALF_UP;
				} else if (mcptAmortMethodDTO.getRoundUp()==MarvelsConstant.ROUND_UP) {
					roundFlag = BigDecimal.ROUND_UP;
				} else if (mcptAmortMethodDTO.getRoundUp()==MarvelsConstant.ROUND_DOWN) {
					roundFlag = BigDecimal.ROUND_DOWN;
				}
				McfeCustomerRepaymentPlanDTO mcfeCustomerRepaymentPlanDTO = repaymentPlanDao.selectAllUncompletedPlan(mcfeCustomerLoanContractDTO.getLoanId()).get(0);
				Date payDate = computeDate(mcfeCustomerLoanContractDTO.getEndDate(),1);
				BigDecimal days = BigDecimal.valueOf(DateUtils.diffTowDate(mcfeCustomerLoanContractDTO.getStartDate(),payDate)+1);
				BigDecimal reInterestOs = dayRate.multiply(mcfeCustomerRepaymentPlanDTO.getPrincipal()).multiply(days).setScale(2, roundFlag);
				mcfeCustomerRepaymentPlanDTO.setInterest(reInterestOs);
				mcfeCustomerRepaymentPlanDTO.setPaymentDate(payDate);
				mcfeCustomerRepaymentPlanDTO.setPaymentDateLate(payDate);
				// 更新还款计划
				repaymentPlanDao.updateRepaymentPlanById(mcfeCustomerRepaymentPlanDTO);

				mcfeCustomerLoanContractDTO.setEndDate(payDate);
				mcfeCustomerLoanContractDTO.setFirstPaymentDate(payDate);
				mcfeCustomerLoanContractDTO.setLastPaymentDate(payDate);
				mcfeCustomerLoanContractDTO.setInterestOs(reInterestOs);
				// 更新合同
				mcfeCustomerLoanContractDao.updateContractBankInfoByLoanId(mcfeCustomerLoanContractDTO);

			}
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>loanId:" + loanId + ",展期异常:", e);
			throw new MarvelsBusinessException(e.getMessage());
		} finally {
			if (!CacheUtils.isNotLock(key, loanId)) {// 如果是锁住的将所释放
				CacheUtils.del(key);
			}
		}
	}

	@Override
	public MarvelsResultDTO newPaymentFlowDetail(LendPaymentConditionDTO conditionDTO) {
		// 获取当前模式的展期费率
		conditionDTO.setExteniosOneDay(ExtensionFunctionEnum.vals()[0]);
		conditionDTO.setExteniosThreeDay(ExtensionFunctionEnum.vals()[1]);
		conditionDTO.setExteniosSevenDay(ExtensionFunctionEnum.vals()[2]);
		Page<RepaymentFlowListDTO> list = repaymentFlowDao.newPaymentFlowDetail(conditionDTO);
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
	}


	@Override
	public HSSFWorkbook downPaymentFlow(LendPaymentConditionDTO conditionDTO) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("还款计划数据一览表");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("进件号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("身份证");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("贷款金额");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("应还款时间");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("应还金额");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("本金");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("滞纳金");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("实还金额");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("实际还款时间");
		cell.setCellStyle(style);
		cell = row.createCell(11);
		cell.setCellValue("还款类型");
		cell.setCellStyle(style);
		cell = row.createCell(12);
		cell.setCellValue("放款日期");
		cell.setCellStyle(style);
		cell = row.createCell(13);
		cell.setCellValue("响应方流水号");
		cell.setCellStyle(style);
		cell = row.createCell(14);
		cell.setCellValue("备注");
		cell.setCellStyle(style);
		BigDecimal paymentAmountTotal=new BigDecimal("0");
		BigDecimal principalTotal=new BigDecimal("0");
		BigDecimal lateAmountTotal=new BigDecimal("0");
		BigDecimal paymentAmountActTotal=new BigDecimal("0");
		Page<RepaymentFlowListDTO> list=repaymentFlowDao.paymentFlowList(conditionDTO);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(i+1);
				row.createCell(1).setCellValue(list.get(i).getLoanId());
				row.createCell(2).setCellValue(list.get(i).getCustName());
				row.createCell(3).setCellValue(list.get(i).getCustCardNo());
				row.createCell(4).setCellValue(list.get(i).getLoanAmount()+"");
				row.createCell(5).setCellValue(DateUtils.getDateStr(list.get(i).getPaymentDate(), "yyyy-MM-dd"));
				row.createCell(6).setCellValue(list.get(i).getPaymentAmount()+"");
				row.createCell(7).setCellValue(list.get(i).getPrincipal()+"");
				row.createCell(8).setCellValue(list.get(i).getLateAmount()+"");
				row.createCell(9).setCellValue(list.get(i).getPaymentAmountAct()+"");
				row.createCell(10).setCellValue(DateUtils.getDateStr(list.get(i).getRealPaymentDate(), "yyyy-MM-dd"));
				if(list.get(i).getRealPaymentDate()!=null){
					if("1".equals(list.get(i).getPaymentType()) && (list.get(i).getRealPaymentDate().after(list.get(i).getPaymentDate()))){
						row.createCell(11).setCellValue("逾期还款");
					}else if("1".equals(list.get(i).getPaymentType()) && (list.get(i).getRealPaymentDate().before(list.get(i).getPaymentDate()))){
						row.createCell(11).setCellValue("正常还款");
					}else if("2".equals(list.get(i).getPaymentType())){
						row.createCell(11).setCellValue("提前结清");
					}
				}
				row.createCell(12).setCellValue(DateUtils.getDateStr(list.get(i).getLoanDate(), "yyyy-MM-dd"));
				row.createCell(13).setCellValue(list.get(i).getOrderNumber());
				row.createCell(14).setCellValue(list.get(i).getRemark());
				paymentAmountTotal=paymentAmountTotal.add(list.get(i).getPaymentAmount());
				principalTotal=principalTotal.add(list.get(i).getPrincipal());
				lateAmountTotal=lateAmountTotal.add(list.get(i).getLateAmount());
				paymentAmountActTotal=paymentAmountActTotal.add(list.get(i).getPaymentAmountAct());
			}
		}
		row = sheet.createRow(list.size() + 1);
		row.createCell(0).setCellValue("合计");
		row.createCell(6).setCellValue(paymentAmountTotal+"");
		row.createCell(7).setCellValue(principalTotal+"");
		row.createCell(8).setCellValue(lateAmountTotal+"");
		row.createCell(9).setCellValue(paymentAmountActTotal+"");
		return wb;
	}

	@Override
	public HSSFWorkbook newDownPaymentFlow(LendPaymentConditionDTO conditionDTO) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("还款计划数据一览表");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("进件号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("身份证");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("手机号");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("贷款金额");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("应还款时间");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("实还金额");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("实还滞纳金");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("实还展期费");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("放款日期");
		cell.setCellStyle(style);
		Page<RepaymentFlowListDTO> list=repaymentFlowDao.newPaymentFlowList(conditionDTO);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(i+1);
				row.createCell(1).setCellValue(list.get(i).getLoanId());
				row.createCell(2).setCellValue(list.get(i).getCustName());
				row.createCell(3).setCellValue(list.get(i).getCustCardNo());
				row.createCell(4).setCellValue(list.get(i).getMobile());
				row.createCell(5).setCellValue(list.get(i).getLoanAmount() + "");
				row.createCell(6).setCellValue(DateUtils.getDateStr(list.get(i).getPaymentDate(), "yyyy-MM-dd"));
				row.createCell(7).setCellValue(list.get(i).getPrincipalAct() + "");
				row.createCell(8).setCellValue(list.get(i).getLateFeeAct() + "");
				row.createCell(9).setCellValue(list.get(i).getExtensionFeeAct() + "");
				row.createCell(10).setCellValue(DateUtils.getDateStr(list.get(i).getLoanDate(), "yyyy-MM-dd"));
			}
		}
		return wb;
	}

	@Override
	public Page<McfeRepaymentFlowDTO> withholdDetailList(LendPaymentConditionDTO conditionDTO)
			throws MarvelsBusinessException {
		int pageSize=conditionDTO.getPageSize();
		int pageNum=conditionDTO.getPageNum();
		if ( pageSize< 1) {
			pageSize=PageConstant.PAGESIZE;
		}
		if (pageNum< 1) {
			pageNum=PageConstant.CURRPAGE;
		}
		PageHelper.startPage(pageNum, pageSize);
		return flowDao.selectRepaymentFlowList(conditionDTO);
	}

	@Override
	public HSSFWorkbook downLoadWithholdDetail(LendPaymentConditionDTO conditionDTO) throws MarvelsBusinessException {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("代扣明细数据一览表");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("进件号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("身份证");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("电话号码");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("产品");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("资金方");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("发起交易时间");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("实际扣款金额");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("还款类型");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("银行名称");
		cell.setCellStyle(style);
		cell = row.createCell(11);
		cell.setCellValue("银行卡号");
		cell.setCellStyle(style);
		cell = row.createCell(12);
		cell.setCellValue("支付通道");
		cell.setCellStyle(style);
		cell = row.createCell(13);
		cell.setCellValue("产品渠道");
		cell.setCellStyle(style);
		cell = row.createCell(14);
		cell.setCellValue("返回信息");
		cell.setCellStyle(style);
		cell = row.createCell(15);
		cell.setCellValue("订单号");
		cell.setCellStyle(style);
		Page<McfeRepaymentFlowDTO> list = flowDao.selectRepaymentFlowList(conditionDTO);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(i+1);
				row.createCell(1).setCellValue(list.get(i).getLoanId());
				row.createCell(2).setCellValue(list.get(i).getCustName());
				row.createCell(3).setCellValue(list.get(i).getCustCardNo());
				row.createCell(4).setCellValue(list.get(i).getMobile());
				row.createCell(5).setCellValue(list.get(i).getProductTypeName());
				row.createCell(6).setCellValue(list.get(i).getFundName());
				row.createCell(7).setCellValue(DateUtils.getDateStr(list.get(i).getTranctionDate(), "yyyy-MM-dd HH:mm:ss"));
				row.createCell(8).setCellValue(list.get(i).getAmountAct() + "");

				if ("1".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("自动代扣");
				} else if ("2".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("当期还款");
				} else if ("3".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("提前结清");
				} else if ("4".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("主动还款");
				} else if ("5".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("下期还款");
				} else if ("6".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("手动代扣");
				} else if ("9".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("支付宝还款");
				} else if ("10".equals(list.get(i).getPayType())) {
					row.createCell(9).setCellValue("支付宝提前结清");
				}

				row.createCell(10).setCellValue(list.get(i).getBankName());
				row.createCell(11).setCellValue(list.get(i).getBankCardNo());
				if ("001".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("快付通（云科贷）");
				} else if ("002".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("宝付（云科贷）");
				} else if ("003".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("宝付（口袋）");
				} else if ("004".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("宝付（渤海）");
				} else if ("005".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("支付宝");
				} else if ("006".equals(list.get(i).getResChannel())) {
					row.createCell(12).setCellValue("运营");
				}
				row.createCell(13).setCellValue(list.get(i).getChannelName());
				row.createCell(14).setCellValue(list.get(i).getRtnResponseMsg());
				row.createCell(15).setCellValue(list.get(i).getRtnOrderNo());
			}
		}
		return wb;
	}
	private Date computeDate(Date firstDate, int tenor) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(firstDate);
		gc.add(GregorianCalendar.MONTH, tenor);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return gc.getTime();
	}

}
